package com.wangsd.web.shop.service;

import com.gitee.fastmybatis.core.query.Query;
import com.gitee.fastmybatis.core.query.Sort;
import com.wangsd.common.basicbean.TreeData;
import com.wangsd.common.exception.BusinessException;
import com.wangsd.web.shop.entity.Customer;
import com.wangsd.web.shop.entity.Merchant;
import com.wangsd.web.shop.entity.Store;
import com.wangsd.web.shop.mapper.MerchantMapper;
import com.wangsd.web.sys.entity.SysCompany;
import com.wangsd.web.sys.entity.SysUser;
import com.wangsd.web.sys.entity.UploadFile;
import com.wangsd.web.sys.service.SysUserService;
import com.wangsd.web.sys.service.UploadFileService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MerchantService {
    @Resource
    private MerchantMapper mapper;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private SysUserService userService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private UploadFileService uploadFileService;

    private Merchant selectByName(String name, Long id) {
        Query query = new Query();
        query.eq("name", name);
        if (id != null) {
            query.notEq("id", id);
        }
        return mapper.getByQuery(query);
    }

    public Merchant selectById(Long id) {
        return mapper.getById(id);
    }

    public Merchant selectByCode(String code) {
        return mapper.getByColumn("code", code);
    }

    public List<Merchant> selectByParentCode(String parentCode) {
        return mapper.listByColumn("parent_code", parentCode);
    }

    public List<Merchant> selectPage(Merchant queryObj, String loginCode) {
        Query query = new Query();
        if (StringUtils.isNotBlank(queryObj.getName())) {
            query.like("name", queryObj.getName());
        }
        if (StringUtils.isNotBlank(loginCode)) {
            query.likeRight("code", loginCode);
        }
        if (queryObj.getParentCode() != null) {
            query.likeRight("code", queryObj.getParentCode());
        }
        query.addSort("sort_no", Sort.DESC);
        List<Merchant> list = mapper.list(query);
        return list;
    }

    public Merchant selectBySubMchId(String subMchId) {
        return mapper.getByColumn("sub_mch_id", subMchId);
    }

    public List<Merchant> selectAllMerchant(String loginCode) {
        Query query = new Query();
        if (StringUtils.isNotBlank(loginCode)) {
            query.likeRight("code", loginCode);
        }
        return mapper.list(query);
    }

    public List<TreeData> selectMerchantTree(boolean isDisable, String loginCode) {
        //返回数据第一级list
        List<TreeData> dataList = new ArrayList();
        List<Customer> customerList = customerService.selectAllCustomer(loginCode);
        List<Merchant> merchantList = selectAllMerchant(loginCode);

        for (Customer service : customerList) {
            //第一级节点
            TreeData map = new TreeData(service.getName(), service.getCode());
            map.setDisabled(isDisable);
            Map<String, List<Merchant>> agentCollect = merchantList.stream().collect(Collectors.groupingBy(Merchant::getParentCode));
            List<Merchant> agents = agentCollect.get(service.getCode());
            if (agents != null) {
                //第二级list
                List agentArray = new ArrayList();
                for (Merchant agent : agents) {
                    //第二级节点
                    TreeData agentObject = new TreeData(agent.getName(), agent.getCode());
                    agentArray.add(agentObject);
                }
                //添加第二级
                map.setChildren(agentArray);
            }
            //添加第一级
            dataList.add(map);
        }
        return dataList;
    }

    public int saveOrUpdate(Merchant entity) {
        Merchant merchant = this.selectByName(entity.getName(), entity.getId());
        if (merchant != null) {
            throw new BusinessException("名称不能重复！");
        }

        if (entity.getId() == null) {
            mapper.saveIgnoreNull(entity);
            entity.setCode(entity.getParentCode() + "-" + entity.getId());
        }
        if (entity.getFilename() != null) {
            UploadFile uploadFile = new UploadFile();
            uploadFile.setMasterId(entity.getId());
            uploadFile.setFilename(entity.getFilename());
            uploadFile.setMasterType(6);
            String fileUrl = uploadFileService.updateUploadFile2(uploadFile);
            entity.setFileUrl(fileUrl);
        }
        if (entity.getFilename2() != null) {
            UploadFile uploadFile = new UploadFile();
            uploadFile.setMasterId(entity.getId());
            uploadFile.setFilename(entity.getFilename2());
            uploadFile.setMasterType(8);
            String fileUrl = uploadFileService.updateUploadFile2(uploadFile);
            entity.setFileUrl2(fileUrl);
        }
        return mapper.updateIgnoreNull(entity);
    }

    public int delete(Long id) {
        Merchant merchant = this.selectById(id);
        List<SysUser> userList = userService.selectByParentCode(merchant.getCode());
        if (userList.size() > 0) {
            throw new BusinessException("商户下面有用户不允许删除！");
        }
        List<Store> storeList = storeService.selectByParentCode(merchant.getCode());
        if (storeList.size() > 0) {
            throw new BusinessException("商户下面有门店不允许删除！");
        }
        return mapper.deleteById(id);
    }

    public List<Merchant> selectAllMerchant(String customerCode, String parentCode) {
        Query query = new Query();
        if (StringUtils.isNotBlank(customerCode)) {
            query.eq("parent_code", customerCode);
        }
        if (StringUtils.isNotBlank(parentCode)) {
            query.likeRight("parent_code", parentCode);
        }
        return mapper.list(query);
    }
}
